package com.ist.acceleratedrawdemo

import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.Rect
import android.graphics.RectF
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.ist.accelerate.DpApi

class MainActivity : AppCompatActivity() {
    private lateinit var mClearBtn: Button
    private lateinit var mAccelerateEnableBtn:Button
    private lateinit var mCloseBtn:Button
    private lateinit var mRedColorPenBtn: Button
    private lateinit var mEraserBtn:Button
    private lateinit var mRectangleBtn:Button
    private lateinit var mDrawCircleBtn: Button
    private lateinit var mDrawOvalBtn:Button
    private lateinit var mDrawArcBtn:Button
    private lateinit var mDrawLineBtn:Button
    private lateinit var mBgColorBtn: Button
    private lateinit var mSeekBar: SeekBar
    private lateinit var mSeekBarText: TextView
    private lateinit var mAccelerateView: AccelerateView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        SpManager.initializeInstance(this)
        mAccelerateView = findViewById(R.id.accelerate_view)
        mClearBtn = findViewById<Button>(R.id.clear)
        mAccelerateEnableBtn = findViewById<Button>(R.id.accelerate_enable)
        mCloseBtn = findViewById<Button>(R.id.close_btn)
        mRedColorPenBtn = findViewById<Button>(R.id.redColor)
        mEraserBtn = findViewById<Button>(R.id.eraser)
        mRectangleBtn = findViewById<Button>(R.id.rectangle)
        mSeekBar = findViewById<SeekBar>(R.id.seekbar)
        mSeekBarText = findViewById<TextView>(R.id.seekbar_text)
        mDrawCircleBtn = findViewById<Button>(R.id.draw_circle)
        mDrawOvalBtn = findViewById<Button>(R.id.draw_oval)
        mDrawArcBtn = findViewById<Button>(R.id.draw_arc)
        mDrawLineBtn = findViewById<Button>(R.id.draw_line)
        mBgColorBtn = findViewById<Button>(R.id.bg_color_btn)

        (findViewById<View>(R.id.versionName) as TextView).text =
            "VersionName: " + DpApi.dpGetSdkVersionName() + ", code: " + DpApi.dpGetSdkVersionCode()

        mAccelerateEnableBtn.text = "启用加速：" + mAccelerateView.isAccelerate
        mAccelerateEnableBtn.setOnClickListener {
            mAccelerateView.isAccelerate = !mAccelerateView.isAccelerate
            mAccelerateEnableBtn.text = "启用加速：" + mAccelerateView.isAccelerate
        }
        mRedColorPenBtn.setOnClickListener {
            var color: Int = SpManager.instance?.getInt(SpManager.KEY_STROKE_COLOR, Color.RED) ?: Color.RED
            val et = findViewById<EditText>(R.id.pen_color_et)
            val content = et.getText().toString()
            if (!TextUtils.isEmpty(content)) {
                try {
                    color = Color.parseColor("#$content")
                } catch (e: IllegalArgumentException) {
                }
            }
            //color = Color.WHITE;
            SpManager.instance?.putInt(SpManager.KEY_STROKE_COLOR, color)
            SpManager.instance?.putFloat(SpManager.KEY_STROKE_WIDTH, 6f)
            mAccelerateView.mode = AccelerateView.MODE_DRAW_PATH
            resetProgress()
        }
        mClearBtn.setOnClickListener{
            mAccelerateView.clearAll()
        }
        mEraserBtn.setOnClickListener {
            SpManager.instance?.putFloat(SpManager.KEY_ERASER_WIDTH, 200f)
            mAccelerateView.mode = AccelerateView.MODE_ERASE
            resetProgress()
        }

        mRectangleBtn.setOnClickListener { mAccelerateView.mode = AccelerateView.MODE_DRAW_SHAPE }

        resetProgress()
        mSeekBar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                mSeekBarText.text = " size=" + mSeekBar.progress.toString()
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {}
            override fun onStopTrackingTouch(seekBar: SeekBar) {
                if (mAccelerateView.mode == AccelerateView.MODE_DRAW_PATH) {
                    SpManager.instance?.putFloat(SpManager.KEY_STROKE_WIDTH, seekBar.progress.toFloat())
                } else if (mAccelerateView.mode == AccelerateView.MODE_DRAW_SHAPE) {
                } else if (mAccelerateView.mode == AccelerateView.MODE_ERASE) {
                    SpManager.instance?.putFloat(SpManager.KEY_ERASER_WIDTH, seekBar.progress.toFloat())
                }
            }
        })

        mDrawCircleBtn.setOnClickListener {
            val paint = Paint(Paint.ANTI_ALIAS_FLAG)
            paint.strokeWidth = 4f
            paint.style = Paint.Style.STROKE
            paint.setColor(Color.BLACK)
            val path = Path()
            path.addCircle(900f, 900f, 500f, Path.Direction.CCW)
            DpApi.dpDrawPath(path, paint)
            DpApi.dpSetRenderBounds(RectF(200f, 200f, 1920f, 1080f))
            DpApi.dpUpdateForbidArea("area1", RectF(300f, 200f, 400f, 300f))
        }
        mDrawOvalBtn.setOnClickListener {
            val paint = Paint(Paint.ANTI_ALIAS_FLAG)
            paint.strokeWidth = 4f
            paint.style = Paint.Style.STROKE
            paint.setColor(Color.BLACK)
            val path = Path()
            path.addOval(900f, 900f, 1400f, 1200f, Path.Direction.CCW)
            DpApi.dpDrawPath(path, paint)
            DpApi.dpUpdateForbidArea("area2", RectF(500f, 200f, 600f, 300f))
        }
        mDrawArcBtn.setOnClickListener {
            val paint = Paint(Paint.ANTI_ALIAS_FLAG)
            paint.strokeWidth = 4f
            paint.style = Paint.Style.STROKE
            paint.setColor(Color.RED)
            val path = Path()
            path.addArc(800f, 800f, 1200f, 1600f, 0f, 90f)
            DpApi.dpDrawPath(path, paint)
            val map = DpApi.dpObtainForbidAreas()
            for ((key, value) in map) {
                Log.d("TAG", "dpObtainForbidAreas: key=$key, value=$value")
            }
        }
        mDrawLineBtn.setOnClickListener {
            val paint = Paint(Paint.ANTI_ALIAS_FLAG)
            paint.strokeWidth = 4f
            paint.style = Paint.Style.STROKE
            paint.setColor(Color.YELLOW)
            val path = Path()
            path.moveTo(700f, 700f)
            path.lineTo(1200f, 700f)
            DpApi.dpDrawPath(path, paint)
            DpApi.dpRemoveForbidArea("area1")
        }
        mBgColorBtn.setOnClickListener {
            val et = findViewById<EditText>(R.id.bg_color_et)
            val content = et.getText().toString()
            if (!TextUtils.isEmpty(content)) {
                try {
                    val cc = Color.parseColor("#$content")
                    mAccelerateView.setBgColor(cc)
                } catch (e: java.lang.IllegalArgumentException) {
                }
            }
            DpApi.dpClearForbidAreas()
        }
    }

    private fun resetProgress() {
        if (mAccelerateView.mode == AccelerateView.MODE_DRAW_PATH) {
            mSeekBar.progress =
                SpManager.instance?.getFloat(SpManager.KEY_STROKE_WIDTH, 10f)?.toInt() ?: 0
        } else if (mAccelerateView.mode == AccelerateView.MODE_DRAW_SHAPE) {
        } else if (mAccelerateView.mode == AccelerateView.MODE_ERASE) {
            mSeekBar.progress =
                SpManager.instance?.getFloat(SpManager.KEY_ERASER_WIDTH, 10f)?.toInt() ?: 0
        }
        mSeekBarText.text = " size=" + mSeekBar.progress.toString()
    }

    override fun onStart() {
        super.onStart()
        DpApi.dpInit()
        DpApi.dpInitInput(Rect(0, 0, 3840, 2160), 3840, 2160,
            3840, 2160, 0.117f, 0.066f)
        DpApi.dpInitRK(Rect(0, 0, 3840, 2160), 0)
    }

    override fun onStop() {
        super.onStop()
        DpApi.dpRelease()
        DpApi.dpReleaseRK()
    }
}